<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Region as region_info;

/*
 * 地区
 * */

class  Region extends Basic
{
    // 初始化
    public function initialize()
    {
        parent::_initialize();

    }

    /*
     * 列表
     */
    public function index()
    {

        $region_info = new region_info();
        $token = Input('post.token');
        $key = Input('post.key');
        $manager = Input('post.manager_id');
        $page = Input('page',1);
        $limit = Input('limit',10);
        $manager_id = decode($manager);
        $manager_info=  $this->search_manager_info($manager_id);
        $region_ids=[];
        if($manager_info['identity'] == 3 || $manager_info['identity'] == 5){
            if($manager_info['region_id']){
                $region_ids =json_decode($manager_info['region_id']);
            }
        }
        if($region_ids){
            $where_six['id'] = ['in', $region_ids];
        }else {
            $where_six['id'] = ['gt', 0];
        }
        if (!$token) {
            json_fail(data('-5', 'token不能为空!'));
        }
        $where = '';
        if ($key) {
            $where['name'] = ['like', '%' . $key . '%'];
        }

        if ($token == encryption(config('token'))) {
          // $list = $region_info->index($where);
            $list = $region_info->index($where,$where_six,$page,$limit);
            if ($list) {
                json_success($list);
            } else {
                json_success([]);
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }

    /*
    * 城市列表
    */
    public function child_index()
    {

        $region_info = new region_info();
        $token = input('post.token');
        $key = input('post.key');
        $pid = input('post.pid');

        if (!$token) {
            json_fail('token不能为空!');
        }
        if (!$pid) {
            json_fail('pid不能为空');
        }
        $where = '';
        if ($key) {
            $where['name'] = ['like', '%' . $key . '%'];
        }else{
            $where['status']=['gt',0];
        }
        if ($token == encryption(config('token'))) {
         $list = $region_info->index_child($where,$pid);
            if ($list) {
                json_success($list);
            } else {
                json_success([]);
            }
        } else {
            json_fail('token验证错误!');
        }
    }



    /*
   * 添加
   */
    public function add()
    {
        //实例化数据库
        $region_info = new region_info();
        $token = input('post.token');
        $name = input('post.name');
        if (!$token) {
            json_fail('token不能为空');
        }
        if (!$name) {
            json_fail('地区名称不能为空');
        }
        $data = input('post.');
        if ($token == encryption(config('token'))) {
            $id = $region_info->allowField(true)->save($data);
            if (!$id) {
                json_success(data('-1', '添加失败'));
            } else {
                json_success(data('1', '添加成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }


    /*
     * 修改
     */
    public function edit()
    {
        //实例化数据库
        $region_info = new region_info();
        $token = input('post.token');
        $id = input('post.id');
        $name = input('post.name');
        if (!$token) {
            json_fail('token不能为空');
        }
        if (!$name) {
            json_fail('地区名称不能为空');
        }
        if (!$id) {
            json_fail(data('-2', 'id不能为空!'));
        }
        $data = input('post.');
        if ($token == encryption(config('token'))) {
            $id = $region_info->allowField(true)->save($data,['id' =>$id]);
            if ($id) {
                json_success(data('1', '修改成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }


    /*
     * 显示修改内容
     */
    public function show_edit()
    {
        //实例化数据库
        $region_info = new region_info();
        $token = input('post.token');
        $id = input('post.id');
        if (!$id) {
            json_fail(data('-1', 'id不能为空!'));
        }
        if (!$token) {
            json_fail(data('-3', 'token不能为空!'));
        }
        if ($token == encryption(config('token'))) {
            $info = $region_info::get($id);
            if (!$info) {
                json_fail(data('-1', '查询失败'));
            } else {
                json_success($info);
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }

    /*
     * 删除
     */
    public function del()
    {
        //实例化数据库
        $region_info = new region_info();
        $token = input('post.token');
        $id = input('post.id');
        if (!$id) {
            json_fail(data('-1', 'id不能为空!'));
        }
        if (!$token) {
            json_fail(data('-3', 'token不能为空!'));
        }
        if ($token == encryption(config('token'))) {
            $data['status'] = 0;
            $id = $region_info->save($data,['id'=>$id]);
            if (!$id) {
                json_fail(data('-1', '删除失败'));
            } else {
                json_success(data('1', '删除成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }

}